package com.cskaoyan.service.system;

import com.cskaoyan.bean.MallRole;
import com.cskaoyan.bean.MallRoleExample;
import com.cskaoyan.mapper.MallManagementMapper;
import com.cskaoyan.mapper.MallPermissionMapper;
import com.cskaoyan.mapper.MallRoleMapper;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.system.PermissionUpdateBo;
import com.cskaoyan.model.bo.system.RoleCreateBo;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.model.vo.system.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-11-30 17:30
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    MallRoleMapper mapper;
    @Autowired
    MallManagementMapper managementMapper;
    @Autowired
    MallPermissionMapper permissionMapper;


    @Override
    public PageListVo list(PageBo pageBo, String name) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        MallRoleExample example = new MallRoleExample();

        example.createCriteria().andDeletedEqualTo(false).andNameLike(name);

        // 设置分页信息
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());

        List<MallRole> list = mapper.selectByExample(example);

        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(list);

        PageListVo vo = new PageListVo();

        vo.setTotal(info.getTotal());
        vo.setLimit(pageBo.getLimit());
        vo.setPage(info.getPageSize());
        vo.setPages(info.getPages());
        vo.setList(info.getList());

        return vo;

    }

    @Override
    public PageListVo options() {

        List<SimpleRole> list = mapper.selectSimpleByExample();

        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(list);

        PageListVo vo = new PageListVo();

        vo.setTotal(info.getTotal());
        vo.setLimit((int) info.getTotal());
        vo.setPage(info.getPageSize());
        vo.setPages(info.getPages());
        vo.setList(info.getList());

        return vo;

    }

    @Override
    public MallRole create(RoleCreateBo bo) {

        // 先查询是否存在同名用户
        Integer repeat = mapper.selectByName(bo.getName());
        if (repeat != 0) return null;

        // 不存在同名用户可以开始增加用户了
        MallRole mallRole = new MallRole();
        mallRole.setName(bo.getName());
        mallRole.setDesc(bo.getDesc());
        mallRole.setAddTime(new Date());
        mallRole.setUpdateTime(new Date());
        mallRole.setEnabled(true);
        mallRole.setDeleted(false);

        int i = mapper.insert(mallRole);

        if (i == 1) return mallRole;

        return null;


    }

    @Override
    public boolean update(MallRole role) {

        MallRoleExample example = new MallRoleExample();

        // 更新管理员信息
        int i = mapper.updateByPrimaryKey(role);

        return i == 1;

    }

    @Override
    public PermissionListVo queryPermissions(Integer roleId) {

        // 根据roleId查出permissionListVo
        List<String> assignedPermissions = permissionMapper.selectPermissionByRoleId(roleId);
        PermissionListVo vo = new PermissionListVo();
        vo.setAssignedPermissions(assignedPermissions);

        // 多级查询查出management

        // 一级查询，查出所有的label_one
        List<SystemPermissions> systemPermissions = managementMapper.selectLevelOne();
        for (SystemPermissions systemPermission : systemPermissions) {
            String labelOne = systemPermission.getId();

            // 二级查询查出所有的label——two
            List<ChildrenEntityOuter> outers = managementMapper.selectLevelTwo(labelOne);
            for (ChildrenEntityOuter outer : outers) {
                String labelTwo = outer.getId();

                // 三级查询查出余下的所有
                List<ChildrenEntityInner> inners = managementMapper.selectLevelThree(labelOne, labelTwo);
                outer.setChildren(inners);
            }
            systemPermission.setChildren(outers);
        }
        vo.setSystemPermissions(systemPermissions);

        return vo;

    }

    @Override
    public PermissionListVo queryAndUpdatePermissions(PermissionUpdateBo bo) {
        Integer roleId = bo.getRoleId();

        // 根据roleId更新permission表
        // 先根据roleId删除
        Integer affected1 = permissionMapper.deleteByRoleId(roleId);
        // 再根据roleId添加
        List<String> permissions = bo.getPermissions();
        for (String permission : permissions) {
            Integer affected2=  permissionMapper.insetPermissions(permission, roleId);
        }


        // 根据roleId查出permissionListVo
        PermissionListVo vo = queryPermissions(roleId);
        return vo;
    }
}
